﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;

namespace Mekiasm
{
    public class MekSmelt : MachineRotateAbleItem,Interface.IDynamicCraft
    {
        public static int Index;

        public Point2 CraftSize => new Point2(1, 1);

        public int[] CraftSlotIndies => new int[] { 0 };

        public int[] FuelCraftSlotIndies => new int[0];


        public int[] ResultSlotIndies => new int[] { 1 };

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 3;

        public int BlockValue => blockValue;

        public MekSmelt() : base( "充能冶炼炉")
        {
            
            textureSlots = new int[] { 72, 74, 75, 74, 91, 73 };
        }
        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            Point3 point = new Point3(x, y, z);
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,null, ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
                    new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
                    new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
                    new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
                    new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
                    new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item),
                    new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item),
               }), new MachineAmountLimitConfig()
               {
                   MaxPower = 10240,
                   InputPowerLimit = 32,
                   OutputPowerLimit = 32
               });//1k eu电量
            ElectricSystem.Add(new SmeltElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new SmeltElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }

        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentSmelt mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentSmelt>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MekSmeltWidget(mcomponent, componentMiner);
            return true;
        }

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("煤炭")
                .SetIngredian(0, "wood")
                .SetResult(0, CoalChunkBlock.Index)
                .SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("橡胶")
                .SetIngredian(0, ILibrary.GetCraftId(Resin.Index))
                .SetResult(0, Rubber.Index)
                .SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("橡胶")
                .SetIngredian(0, ILibrary.GetCraftId(RubberWood.Index))
                .SetResult(0, Rubber.Index)
                .SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("锡锭")
                .SetIngredian(0, ILibrary.GetCraftId(OreTin.Index))
                .SetResult(0, TinIngnot.Index)
                .SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("铜锭")
                .SetIngredian(0, ILibrary.GetCraftId(MalachiteChunkBlock.Index))
                .SetResult(0, CopperIngotBlock.Index)
                .SetRemainNeedCheck());
            mekRecipe.Add(new MekRecipe(1, 0, 1).SetName("铁锭")
                .SetIngredian(0, ILibrary.GetCraftId(IronOreChunkBlock.Index))
                .SetResult(0, IronIngotBlock.Index)
                .SetRemainNeedCheck());
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            InventorySlotWidget inventory, inventory1, inventory2;
            MekProgressWidget craftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.down) { HorizontalAlignment = WidgetAlignment.Center,Size=new Vector2(12,48) };
            StackPanelWidget MainPanelVertical = new StackPanelWidget() { Direction = LayoutDirection.Vertical,VerticalAlignment=WidgetAlignment.Center,HorizontalAlignment=WidgetAlignment.Center};
            inventory = new InventorySlotWidget() { Size = new Vector2(48, 48), BevelColor = Color.Black, HorizontalAlignment = WidgetAlignment.Center };
            inventory.AssignInventorySlot(energyMachine, 0);
            inventory1 = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(10, 0) };
            inventory1.AssignInventorySlot(energyMachine, 1);
            inventory2 = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(10, 0) };
            inventory2.AssignInventorySlot(energyMachine, 2);
            StackPanelWidget ResultHorizontalPanel = new StackPanelWidget() { Direction = LayoutDirection.Horizontal };
            ResultHorizontalPanel.Children.Add(inventory1);
            ResultHorizontalPanel.Children.Add(inventory2);
            inventory.Margin = new Vector2(0, 10);
            craftProgress.Margin = new Vector2(0, 10);
            ResultHorizontalPanel.Margin = new Vector2(0, 10);
            MainPanelVertical.Children.Add(inventory);
            MainPanelVertical.Children.Add(craftProgress);
            MainPanelVertical.Children.Add(ResultHorizontalPanel);
            canvas.AddChildren(MainPanelVertical);
            Tick += () => {
                craftProgress.SetProgress(energyMachine.CraftProgress);
            };

        }
    }
}
